package com.gorkr.labuladong.data_struct.array.binary_search;

import org.junit.jupiter.api.Test;

/**
 * @author gorkr
 * @date 2022/06/07 20:34
 **/
public class M875KokoEatingBananas {
    public int minEatingSpeed(int[] piles, int h) {
        int max = 0;
        for (int pile :
                piles) {
            if (max < pile) {
                    max = pile;
            }
        }
        double left = 0, right = max+1;
        double tmp;
        int mid;
        while (left<right){
            mid =(int)( (left+right) / 2);
            tmp = func(mid, piles);
            if(tmp<=h){
                right = mid;
            } else{
                left = mid + 1;
            }
        }


        return (int)(left);
    }

    // x=吃的速度 f(x)等于吃完需要多久  找小于8的最大值的 x
    public int func(double x, int[] piles){
        int res = 0;
        for (int pile: piles) {
            res+=Math.ceil(pile/x);
//            System.out.println(res);
        }
        return res;
    }

    @Test
    public void test(){
//        int[] piles = {30,11,23,4,20};
//        int h = 5;

        int[] piles = {312884470};
        int h = 312884469;

//        int[] piles = {3,6,7,11};
//        int h = 8;
        System.out.println(minEatingSpeed(piles, h));
    }
}
    